package org.example.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

@RestController
@CrossOrigin
public class Controller {

    @PostMapping("/upload")
    public  HashMap upload(@RequestParam("file") MultipartFile file){
        HashMap<Object, Object> hashMap = new HashMap<>();
          try {
              String string = new String(file.getBytes(),"UTF-8");
              hashMap.put("文件名：",file.getOriginalFilename());
              hashMap.put("文件大小：",file.getSize());
              hashMap.put("文件类型：",file.getContentType());
              hashMap.put("文件内容：",string);

          } catch (IOException e) {
              throw new RuntimeException(e);
          }
          try {
              FileOutputStream outputStream = new FileOutputStream("C:/Users/86193/Desktop/SS/写入1.txt");
              outputStream.write(file.getBytes());
              System.out.println("写入成功");
              outputStream.close();
          }
          catch (Exception e){
              e.printStackTrace();
          }
        return  hashMap;
    }


}
